package com.snowleopard.common.label;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//Spring Service
@Service
@Transactional
public class LabelManager {

	private static Logger logger = LoggerFactory.getLogger(LabelManager.class);

	@Autowired
	private LabelDao labelDao;

	@Transactional(readOnly = true)
	public List<Label> getAll() {
		return labelDao.getAll();
	}

	public Object[][] findEnglishLabels() {
		logger.info("Get english language...");
		List<Label> labels = labelDao.getAll();
		if (labels == null)
			return new Object[][] {};
		Object[][] results = new Object[labels.size()][];
		int i = 0;
		for (Label commonLabelEn : labels) {

			results[i] = new Object[2];
			results[i][0] = commonLabelEn.getLabelCode();
			if (commonLabelEn.getLabelEng() == null) {
				results[i][1] = commonLabelEn.getLabelCode();
			} else {
				results[i][1] = commonLabelEn.getLabelEng();
			}
			i++;
		}
		return results;
	}

	public Object[][] findSimplifiedChineseLabels() {
		logger.info("Get Simplified Chinese language...");

		List<Label> labels = labelDao.getAll();
		if (labels == null)
			return new Object[][] {};
		Object[][] results = new Object[labels.size()][];
		int i = 0;
		for (Label commonLabelEn : labels) {
			results[i] = new Object[2];
			results[i][0] = commonLabelEn.getLabelCode();
			if (commonLabelEn.getLabelZhs() == null) {
				if (commonLabelEn.getLabelEng() == null) {
					results[i][1] = commonLabelEn.getLabelCode();
				} else {
					results[i][1] = commonLabelEn.getLabelEng();
				}
			} else {
				results[i][1] = commonLabelEn.getLabelZhs();
			}
			i++;
		}
		return results;
	}

	public Object[][] findTraditionalChineseLabels() {
		logger.info("Get Traditional Chinese language...");

		List<Label> labels = labelDao.getAll();
		if (labels == null)
			return new Object[][] {};
		Object[][] results = new Object[labels.size()][];
		int i = 0;
		for (Label commonLabelEn : labels) {
			results[i] = new Object[2];
			results[i][0] = commonLabelEn.getLabelCode();
			if (commonLabelEn.getLabelZht() == null) {
				if (commonLabelEn.getLabelEng() == null) {
					results[i][1] = commonLabelEn.getLabelCode();
				} else {
					results[i][1] = commonLabelEn.getLabelEng();
				}
			} else {
				results[i][1] = commonLabelEn.getLabelZht();
			}
			i++;
		}
		return results;
	}

	public Label getLabelByLabelCode(String labelCode) {
		Label label = labelDao.findUniqueBy("labelCode", labelCode);
		return label;
	}

}
